<?php

/**
 * boleta actions.
 *
 * @package    aeropostal
 * @subpackage boleta
 * @author     Your name here
 */
class boletaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
        $pager = new sfPropelPager('Boleta', sfConfig::get('app_max_boletas'));
        $c = new Criteria;
        if($this->getUser()->hasCredential(array('analista'),false)){
          $in_query = 'boleta.numboleta IN (SELECT clave FROM log where estructura like "Boleta" and usuario="'.$this->getUser().'")';
          $c->add(BoletaPeer::NUMBOLETA, $in_query, Criteria::CUSTOM);  
        }
	$pager->setCriteria($c);
	// Se establece que la pagina principal del index sera la 1
	$pager->setPage($this->getRequestParameter('page', 1));
	$pager->init();
	 // Se Prepara la informaci�n para usarla en la vista.
	$this->pager = $pager;
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->Boleta = BoletaPeer::retrieveByPk($request->getParameter('proveedor'),$request->getParameter('numboleta'));
    $this->forward404Unless($this->Boleta);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new BoletaForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new BoletaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($Boleta = BoletaPeer::retrieveByPk($request->getParameter('proveedor'), $request->getParameter('numboleta')), sprintf('Object Boleta does not exist (%s).', $request->getParameter('proveedor'), $request->getParameter('numboleta')));
    $this->form = new BoletaForm($Boleta);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($Boleta = BoletaPeer::retrieveByPk($request->getParameter('proveedor'),$request->getParameter('numboleta')), sprintf('Object Boleta does not exist (%s).', $request->getParameter('proveedor'), $request->getParameter('numboleta')));
    $this->form = new BoletaForm($Boleta);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($Boleta = BoletaPeer::retrieveByPk($request->getParameter('proveedor'),$request->getParameter('numboleta')), sprintf('Object Boleta does not exist (%s).', $request->getParameter('proveedor'),$request->getParameter('numboleta')));
    $Boleta->delete();
    $numero = $request->getParameter('numboleta');
    LogPeer::logBorrar($numero,$this->getUser(),'Boleta');
    $this->redirect('boleta/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
        try {
          $Boleta = $form->save();
          $numero = $request->getPostParameter('boleta[numBoleta]');
          LogPeer::log($numero,$this->getUser(),'Boleta');
          $this->redirect('boleta/index');  
        }catch (Exception $e){
            $this->getUser()->setFlash('error', 'No se puede registrar esta boleta, el número de boleta ya existe.');
            return sfView::NONE;
        }
    }
  }
  
    public function executeMostrarEstaciones(sfWebRequest $request)
    {
            $cargo = $request->getParameter('vuelo');
            $respuesta = VueloPeer::doSelectEstacionByNumVuelo($cargo);
            $this->getResponse()->setContent($respuesta);
            return sfView::NONE;
    }
    public function executeMostrarOrigen(sfWebRequest $request)
    {
            $cargo = $request->getParameter('vuelo');
            $respuesta = VueloPeer::doSelectOrigenByNumVuelo($cargo);
            $this->getResponse()->setContent($respuesta);
            return sfView::NONE;
    }
    public function executeMostrarMedida(sfWebRequest $request)
    {
            $medida = $request->getParameter('uMedida');
            $respuesta = ProveedorPeer::doSelectUnidMedidaProveedorByRif($medida);
            $this->getResponse()->setContent($respuesta);
            return sfView::NONE;
    }
}
